import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Color;

class UDButton extends JButton{


	int state = 0;

	public UDButton(String text){
		super(text);


	}

	void OnClicked(){


		if(state==0){
			this.setForeground(Color.RED);
			state=1;
		}
		else
		{
			this.setForeground(Color.BLACK);
			state=0;
		}


	}
}

public class ElevatorUI extends JDialog implements ActionListener{


	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	JLabel[] lblTops = new JLabel[10];

	JLabel[] lblOuts = new JLabel[11];

	JLabel[] lblUps = new JLabel[11];
	JLabel[] lblDowns = new JLabel[11];

	UDButton[] btnElevator1 = new UDButton[100];
	UDButton[] btnElevator2 = new UDButton[100];

	ElevatorStatus leftElevator = new ElevatorStatus();
	ElevatorStatus rightEleavtor = new ElevatorStatus();

	FloorStatus[] Floor = new FloorStatus[11];
	


	int flag=-1;
	int desty=1;


	public static void main(String[] args) {
		try {
			ElevatorUI dialog = new ElevatorUI();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ElevatorUI() {
		setBounds(100, 100, 450, 538);   //(x, y , width, height)
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 30));

		JPanel panel_0 = new JPanel();
		contentPanel.add(panel_0, BorderLayout.NORTH);


		JPanel panel = new JPanel();
		contentPanel.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));


		for(int i=1;i<=10;i++){
			Floor[i] = new FloorStatus();
		}
		
		JPanel panel_1 = new JPanel(new GridLayout(3,1));
		panel_1.setBackground(Color.GRAY);
		panel_1.setForeground(Color.WHITE);
		panel.add(panel_1, BorderLayout.NORTH);

		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}));
		panel_0.add(comboBox);

		JPanel panel_up = new JPanel(new GridLayout(1,10));
		//		panel_up.setBackground(Color.GRAY);
		panel_up.setForeground(Color.WHITE);
		panel_1.add(panel_up);

		for(int i=1;i<=10;i++){

			lblUps[i] = new JLabel("▲");
			lblUps[i].setForeground(Color.WHITE);
			panel_up.add(lblUps[i]);
		}

		JPanel panel_down = new JPanel(new GridLayout(1,10));
		//		panel_down.setBackground(Color.GRAY);
		panel_down.setForeground(Color.WHITE);
		panel_1.add(panel_down);

		for(int i=1;i<=10;i++){

			lblDowns[i] = new JLabel("▼");
			lblDowns[i].setForeground(Color.WHITE);
			panel_down.add(lblDowns[i]);
		}


		JButton btnNewButton = new JButton("위로");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int pressed = comboBox.getSelectedIndex()+1;
				lblUps[pressed].setForeground(Color.RED);
				Floor[pressed].upButtonPressed();
			}
		});
		panel_0.add(btnNewButton);



		JButton btnNewButton_1 = new JButton("아래로");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int pressed = comboBox.getSelectedIndex()+1;
				lblDowns[pressed].setForeground(Color.RED);
				Floor[pressed].downButtonPressed();
			}
		});
		panel_0.add(btnNewButton_1);



		JPanel panel_num = new JPanel(new GridLayout(1,10));
		panel_num.setBackground(Color.GRAY);
		panel_num.setForeground(Color.WHITE);
		panel_1.add(panel_num);


		for(int i=1;i<=10;i++){

			lblOuts[i] = new JLabel(" "+Integer.toString(i));
			lblOuts[i].setForeground(Color.WHITE);
			panel_num.add(lblOuts[i]);
		}



		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(null);

		JLabel label1 = new JLabel("엘리베이터 1호기");
		label1.setForeground(Color.BLUE);
		label1.setBackground(new Color(30, 144, 255));
		label1.setHorizontalAlignment(SwingConstants.CENTER);
		label1.setBounds(10, 10, 135, 70);
		panel_2.add(label1);

		JLabel label2 = new JLabel("엘리베이터 2호기");
		label2.setForeground(Color.PINK);
		label2.setHorizontalAlignment(SwingConstants.CENTER);
		label2.setBackground(new Color(30, 144, 255));
		label2.setBounds(253, 10, 135, 70);
		panel_2.add(label2);

		final JLabel lblElevatorFloor_1a = new JLabel("1");
		lblElevatorFloor_1a.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElevatorFloor_1a.setBounds(10, 42, 28, 50);
		lblElevatorFloor_1a.setForeground(Color.BLUE);
		panel_2.add(lblElevatorFloor_1a);

		JLabel lblElevatorFloor_1 = new JLabel("층");
		lblElevatorFloor_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElevatorFloor_1.setBounds(10, 42, 42, 50);
		panel_2.add(lblElevatorFloor_1);

		JLabel lblElevatorState_1a = new JLabel("▲");
		lblElevatorState_1a.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElevatorState_1a.setForeground(Color.BLUE);
		lblElevatorState_1a.setBounds(67, 42, 55, 50);
		panel_2.add(lblElevatorState_1a);

		JLabel lblElevatorState_1 = new JLabel("상태");
		lblElevatorState_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElevatorState_1.setBackground(Color.LIGHT_GRAY);
		lblElevatorState_1.setBounds(67, 42, 87, 50);
		panel_2.add(lblElevatorState_1);

		JLabel lblElevatorFloor_2a = new JLabel("7");
		lblElevatorFloor_2a.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElevatorFloor_2a.setBounds(253, 42, 28, 50);
		lblElevatorFloor_2a.setForeground(Color.PINK);
		panel_2.add(lblElevatorFloor_2a);

		JLabel lblElevatorFloor_2 = new JLabel("층");
		lblElevatorFloor_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElevatorFloor_2.setBounds(253, 42, 42, 50);
		panel_2.add(lblElevatorFloor_2);

		JLabel lblElevatorState_2a = new JLabel("▼");
		lblElevatorState_2a.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElevatorState_2a.setForeground(Color.PINK);
		lblElevatorState_2a.setBounds(307, 42, 55, 50);
		panel_2.add(lblElevatorState_2a);

		JLabel lblElevatorState_2 = new JLabel("상태");
		lblElevatorState_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElevatorState_2.setBackground(Color.LIGHT_GRAY);
		lblElevatorState_2.setBounds(307, 42, 87, 50);
		panel_2.add(lblElevatorState_2);


		for(int y=0; y<=1;y++){
			for(int i=0;i<=4;i++){

				btnElevator1[5*y+i+1] = new UDButton(Integer.toString(5*y+i+1));
				btnElevator1[5*y+i+1].setBounds(30+56*y, 80+40*i, 48, 29);
				btnElevator1[5*y+i+1].addActionListener(this);
				panel_2.add(btnElevator1[5*y+i+1]);
			}
		}


		for(int y=0; y<=1;y++){
			for(int i=0;i<=4;i++){

				btnElevator2[5*y+i+1] = new UDButton(Integer.toString(5*y+i+1));
				btnElevator2[5*y+i+1].setBounds(275+56*y, 80+40*i, 48, 29);
				btnElevator2[5*y+i+1].addActionListener(this);
				panel_2.add(btnElevator2[5*y+i+1]);

			}
		}


		Thread worker = new Thread(){

			public void run(){

				while(true){
					try{

						desty = getUps(leftElevator.getCurrentFloor());
//						int [] downarr = getDowns();

						
						if(leftElevator.getCurrentFloor() == desty){
							lblUps[desty].setForeground(Color.white);
							Floor[desty].update(flag);
							Thread.sleep((long)(300));
							flag = 0;
							
						}
						
						if (leftElevator.getCurrentFloor() < desty) flag = -1;
						if (leftElevator.getCurrentFloor() > desty) flag = 1;
						
						if (flag > 0 ){
							Thread.sleep((long)(1000));
							leftElevator.setCurrentFloor(leftElevator.getCurrentFloor() - 1);
						}
						if (flag < 0 ){
							Thread.sleep((long)(1000));
							leftElevator.setCurrentFloor(leftElevator.getCurrentFloor() + 1);
						}


					}catch(InterruptedException ex){}

					SwingUtilities.invokeLater(new Runnable() {

						public void run() {

							for(int i=1; i<=10;i++){
								if(i==leftElevator.getCurrentFloor()){

									lblOuts[i].setForeground(Color.blue);
								}
								else
								{
									lblOuts[i].setForeground(Color.WHITE);
								}

							}
							lblElevatorFloor_1a.setText( Integer.toString(leftElevator.getCurrentFloor()) );
						}

					});
				}
			}
		};
		worker.start();



		JPanel panel_3 = new JPanel();
		contentPanel.add(panel_3, BorderLayout.SOUTH);

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);





	}




	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		((UDButton) e.getSource()).OnClicked();

		//e.getsaddscommand 이용하자

	}




	public void Elevator1FloorChange(int num) {
		// TODO Auto-generated method stub

	}




	public void Elevator2FloorChange(int num) {
		// TODO Auto-generated method stub

	}


	public int getUps(int r)
	{
		int desty=r;
		
		for(int i=r;i<=10;i++){

			if(Floor[i].getUpButton()==true)
			{	 
				desty=i;
				break;

			}
		}

		return desty;
	}

	public int[] getDowns()
	{
		int arr[] = new int[11];

		for(int i=1;i<=10;i++){

			if(Floor[i].getDownButton()==true)
				arr[i]=1;
			else
				arr[i]=0;

		}
		return arr;
	}
}
